

#====================================================================
#
#		STRING MANIPULATION
#
#====================================================================


#--------------------------------------------------------------------
function Bytes2AsciiString
{
	param
	(
		  [byte[]]			$Bytes			= $( throw "Byte array is required." )
	)
	return [Text.Encoding]::ASCII.GetString( $Bytes )
}


#--------------------------------------------------------------------
function AsciiString2Bytes
{
	param
	(
		  [string]			$String			= $( throw "Ascii string is required." )
	)
	return [Text.Encoding]::ASCII.GetBytes( $String )
}


#--------------------------------------------------------------------
function Bytes2UnicodeString
{
	param
	(
		  [byte[]]			$Bytes			= $( throw "Byte array is required." )
	)
	return [Text.Encoding]::Unicode.GetString( $Bytes )
}


#--------------------------------------------------------------------
function UnicodeString2Bytes
{
	param
	(
		  [string]			$String			= $( throw "Unicode string is required." )
	)
	return [Text.Encoding]::Unicode.GetBytes( $String )
}


#--------------------------------------------------------------------
function Bytes2Base64String
{
	param
	(
		  [byte[]]			$Bytes			= $( throw "Byte array is required." )
	)
	return [convert]::ToBase64String( $Bytes )
}


#--------------------------------------------------------------------
function Base64String2Bytes
{
	param
	(
		  [string]			$String			= $( throw "Base64 string is required." )
	)
	return [convert]::FromBase64String( $String )
}


#--------------------------------------------------------------------
function Trim-String
{
	param
	(
		  [string]			$String			= $( throw "String is required." )
		, [string]			$Characters		= " "
	)
	[string] $ch = ""
	while( $true )
	{
		if( $String.Length -eq 0 ) { break; }
		$ch = $String.Substring( 0, 1 )
		if( $Characters.Contains( $ch ) )
			{ $String = $String.SubString( 1, ($String.Length - 1) ) }
		else
			{ break; }
	}
	while( $true )
	{
		if( $String.Length -eq 0 ) { break; }
		$ch = $String.Substring( ($String.Length - 1), 1 )
		if( $Characters.Contains( $ch ) )
			{ $String = $String.SubString( 0, ($String.Length - 1) ) }
		else
			{ break; }
	}
	return $String
}


